from typing import List, Dict, Tuple, Set
import logging

class CitationValidator:
    """引用验证器"""
    
    def __init__(self):
        self.logger = logging.getLogger(__name__)

    def validate_integrity(self, citations: List[Dict]) -> Tuple[bool, Set[int]]:
        """验证引用完整性"""
        try:
            if not citations:
                return False, set()

            # 验证编号连续性
            numbers = {cite['number'] for cite in citations}
            expected = set(range(1, max(numbers) + 1))
            missing = expected - numbers
            
            if missing:
                self.logger.warning(f"缺失引用编号: {missing}")
                return False, missing

            # 验证字段完整性
            for cite in citations:
                if not self._validate_citation_fields(cite):
                    return False, missing

            return True, set()
            
        except Exception as e:
            self.logger.error(f"引用验证失败: {str(e)}")
            return False, set()

    def _validate_citation_fields(self, citation: Dict) -> bool:
        """验证单个引用的字段完整性"""
        required_fields = ['number', 'title', 'venue', 'year', 'chunk_id']
        return all(
            citation.get(field) is not None 
            for field in required_fields
        )
